#!/bin/sh
# Real Server管理
AppName="Real Server"
VIP=192.168.100.200
NIC=lo:0
IPAddress=$(ip addr | grep $NIC | sed 's/^[[:space:]]*//')

fstatus()
{
    if [ -z "$IPAddress" ]; then
        echo "$AppName 未启动"
    else
        echo "$IPAddress"
    fi

    echo "内核参数："
    sysctl -a | egrep "lo.arp_ignore|lo.arp_announce|all.arp_ignore|all.arp_announce" 
}

fstart()
{   
    if [ -z "$IPAddress" ]; then
        echo "启动 $AppName"
        ifconfig $NIC $VIP broadcast $VIP netmask 255.255.255.255 up
        route add -host $VIP dev $NIC
    else
        echo "$AppName 正在运行"
    fi

    echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
    sysctl -p > /dev/null
}

fstop()
{
    if [ -n "$IPAddress" ]; then
        echo "停止 $AppName"
        ifconfig $NIC down
    else
        echo "$AppName 已停"
    fi

    echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
    sysctl -p > /dev/null
}


ScriptFile=$(basename $0)
case "$1" in
    "status"    ) fstatus ;;
    "start"     ) fstart ;;
    "stop"      ) fstop ;;
    "restart"   ) fstop && fstart ;;
    * )
        echo "$ScriptFile status               查询 $AppName 状态"
        echo "$ScriptFile start                启动 $AppName"
        echo "$ScriptFile stop                 停止 $AppName"
        echo "$ScriptFile restart              重启 $AppName"
        ;;
esac
